<template>
  <div class="notification-toast" :class="type">
    <div class="toast-content">
      <span class="message">{{ message }}</span>
      <el-button 
        v-if="showClose" 
        class="close-btn" 
        type="text" 
        @click="closeToast"
      >
        <el-icon><Close /></el-icon>
      </el-button>
    </div>
    <div 
      v-if="duration > 0" 
      class="progress-bar" 
      :style="{ 
        animationDuration: duration + 'ms',
        backgroundColor: type === 'success' ? '#67c23a' : type === 'error' ? '#f56c6c' : '#409eff'
      }"
    ></div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElButton, ElIcon } from 'element-plus'
import { Close } from '@element-plus/icons-vue'

const props = defineProps({
  message: {
    type: String,
    required: true
  },
  type: {
    type: String,
    default: 'info', // success, error, info, warning
    validator: (value) => ['success', 'error', 'info', 'warning'].includes(value)
  },
  duration: {
    type: Number,
    default: 3000
  },
  showClose: {
    type: Boolean,
    default: true
  },
  onClose: {
    type: Function,
    default: () => {}
  }
})

const emit = defineEmits(['close'])

const closeToast = () => {
  emit('close')
  if (props.onClose) {
    props.onClose()
  }
}

onMounted(() => {
  if (props.duration > 0) {
    setTimeout(() => {
      closeToast()
    }, props.duration)
  }
})
</script>

<style scoped>
.notification-toast {
  position: fixed;
  top: 20px;
  right: 20px;
  min-width: 300px;
  padding: 16px;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  z-index: 9999;
  background-color: #fff;
  border: 1px solid;
}

.notification-toast.success {
  border-color: #e1f3d8;
  background-color: #f0f9eb;
}

.notification-toast.error {
  border-color: #fde2e2;
  background-color: #fef0f0;
}

.notification-toast.info {
  border-color: #d9ecff;
  background-color: #ecf5ff;
}

.notification-toast.warning {
  border-color: #faecd8;
  background-color: #fdf6ec;
}

.toast-content {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.message {
  flex: 1;
  font-size: 14px;
  line-height: 1.5;
}

.close-btn {
  margin-left: 16px;
  padding: 0;
  height: auto;
}

.progress-bar {
  height: 3px;
  margin-top: 12px;
  animation: progress linear;
  transform-origin: left;
}

@keyframes progress {
  from {
    transform: scaleX(1);
  }
  to {
    transform: scaleX(0);
  }
}
</style>